home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / xlib20b.zip / EXAMPLE3.ASM < prev    next >
Assembly Source File  |  1993-10-11  |  4KB  |  90 lines

  1. ;The following library should be combined with XLIB.LIB using the Microsoft
  2. ;LINK and LIB utilities.  If BASIC is to be executed from the QBX
  3. ;environment, then a quick library must be loaded with the environment.  See
  4. ;BASIC documentation for instructions.
  5.  
  6.  
  7.                .MODEL         LARGE,PASCAL
  8.                .386P
  9.  
  10.                INCLUDE        XLIB.INC
  11.  
  12. CSEG           SEGMENT PARA PUBLIC USE16 'CODE'
  13.                ASSUME CS:CSEG, DS:DSEG
  14.  
  15. ;Function to calculate linear address from segment address on stack.
  16. ;Returns linear address in DX:AX.
  17. LINADR         PROC FAR PUBLIC,
  18.                SEGADR:DWORD                  ;Segment address of variable
  19.                XOR            EAX,EAX        ;Clear high words
  20.                XOR            EDX,EDX
  21.                MOV            AX,WORD PTR SEGADR[0]
  22.                MOV            DX,WORD PTR SEGADR[2]
  23.                SHL            EDX,4          ;Calculate linear address
  24.                ADD            EDX,EAX
  25.                MOV            AX,DX
  26.                SHR            EDX,16         ;Return linear address in DX:AX
  27.                RET
  28. LINADR         ENDP
  29.  
  30. ;Structure defining control block for SUMARRAY.
  31. ARRAYDATA      STRUCT
  32.   CONDCODE     DWORD          0              ;Condition code
  33.   N            DWORD          0              ;Number of elements to sum
  34.   ADDRESS      DWORD          0              ;Address of first element
  35.   SUM          DWORD          0              ;Sum of array elements
  36. ARRAYDATA      ENDS
  37.  
  38. ;Real-mode interface to SUMARRAY32.  Segment address of control block having
  39. ;structure ARRAYDATA should be on the stack.
  40. SUMARRAY       PROC FAR PUBLIC,
  41.                CBSEGADR:DWORD                ;Control block segment address
  42.                PUSH           DS
  43.                PUSHW          DSEG
  44.                POP            DS
  45.                XOR            EAX,EAX        ;Clear high words
  46.                XOR            EDX,EDX
  47.                MOV            AX,WORD PTR CBSEGADR[2]
  48.                MOV            DX,WORD PTR CBSEGADR[0]
  49.                SHL            EAX,4          ;Calculate linear address
  50.                ADD            EAX,EDX
  51.                MOV            CCODEPTR,EAX   ;Reset condition code address
  52.                POP            DS             ;Pop calling DS
  53.                PUSHD          OFFSET SUMARRAY32
  54.                CALL           ENTERPM        ;Execute SUMARRAY32 in protected
  55.                RET
  56. SUMARRAY       ENDP
  57.  
  58. CSEG           ENDS
  59.  
  60. TSEG           SEGMENT PARA PUBLIC USE32 'CODE'
  61.                ASSUME CS:TSEG, SS:TSEG, DS:TSEG, ES:TSEG, FS:DSEG, GS:DGROUP
  62.  
  63. ;Sum the elements of a single precision array.  Array parameters are stored
  64. ;in a control block having structure of ARRAYDATA.  The linear address of the
  65. ;control block is stored at CCODEPTR.  An error code of -1 is returned in the
  66. ;condition code of the control block if the number of array elements is zero.
  67. ;XLIB places an error code in the control block if an FPU exception occurs
  68. ;while calculating the sum.  This error code will have the FPU status word in
  69. ;the high word and the XLIB FPU error code in the low word.  Observe that this
  70. ;routine will be called with DS = FLATDSEL (flat-model data descriptor) and
  71. ;FS = DSEGSEL (DSEG data descriptor).
  72. SUMARRAY32     PROC NEAR
  73.                MOV            EBX,FS:CCODEPTR               ;Get control block
  74.                MOV            EDX,ARRAYDATA.ADDRESS[EBX]    ;Get array address
  75.                MOV            ESI,ARRAYDATA.N[EBX]          ;Get N
  76.                SUB            ESI,1
  77.                JB             NODATA                        ;Error:  N = 0
  78.                FLDZ                                         ;Initialize sum
  79. SUMLOOP:       FADD           DWORD PTR [EDX+4*ESI]
  80.                SUB            ESI,1
  81.                JAE            SUMLOOP
  82.                FSTP           ARRAYDATA.SUM[EBX]            ;Save sum
  83.                RET
  84. NODATA:        MOV            ARRAYDATA.CONDCODE[EBX],-1    ;Record error code
  85.                RET
  86. SUMARRAY32     ENDP
  87.  
  88. TSEG           ENDS
  89.                END
  90.